package com.jky.stmall.manger.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jky.stmall.manger.entity.Category;
import com.jky.stmall.manger.mapper.CategoryMapper;
import com.jky.stmall.manger.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Service实现类
 * 1.需要实现Service接口
 * 2.添加Service注解
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public int add(Category category) throws Exception {
        //1.参数验证
        if (category.getName() == null || category.getName().length() == 0) {
            throw new Exception("分类名不能为空");
        }
        if (category.getRecom() == null) {
            throw new Exception("推荐不能为空");
        }
        //2.逻辑处理
        //3.访问数据库
        return categoryMapper.insert(category);
    }

    @Override
    public int remove(int id) throws Exception {
        Category category = categoryMapper.selectById(id);
        if (category == null) {
            throw new Exception("id不存在！！！");
        }

        return categoryMapper.delete(id);
    }

    @Override
    public int edit(Category category) throws Exception {
        Category category1 = categoryMapper.selectById(category.getId());
        if (category1 == null) {
            throw new Exception("ID不存在");
        }
        return categoryMapper.update(category);
    }

    @Override
    public Category getById(Integer id) {

        return categoryMapper.selectById(id);
    }

    @Override
    public List<Category> search(Category category) {
        return categoryMapper.select(category);
    }

    @Override
    public PageInfo searchByPage(Integer page, Integer limit, Category category) {
        //分三步
        //1.启动分页
        PageHelper.startPage(page, limit);
        //2.调用Mapper
        List<Category> list = categoryMapper.select(category);
        //3.构造Pageinfo
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }
}
